import 'package:get_storage/get_storage.dart';

enum StoreKeys {
  token,
  refreshToken,
  userInfo,
  account,
  password,
  systemTip,
  orderMsgTip,
  orderTip,
  language,
  currency
}

class StorageUtils {
  static final StorageUtils _storage = StorageUtils._internal();
  final GetStorage _box = GetStorage();

  GetStorage get box => _box;

  StorageUtils._internal();

  factory StorageUtils() => _storage;

  // setToken, getToken
  setToken(String token) => _box.write(StoreKeys.token.toString(), token);

  String? getToken() => _box.read<String>(StoreKeys.token.toString());

  setUserInfo(Map<String, dynamic> userInfo) =>
      _box.write(StoreKeys.userInfo.toString(), userInfo);

  Map<String, dynamic>? getUserInfo() =>
      _box.read(StoreKeys.userInfo.toString());

  ///清除缓存
  Future<void> clearAll() async {
    // return _box.erase();
    await _box.remove(StoreKeys.userInfo.toString());
    await _box.remove(StoreKeys.token.toString());
    return;
  }

  isLogin() {
    // if (getUserInfo() != null && getToken() != null) {

    if (getToken() != null) {
      return true;
    }
    return false;
  }

  removeKey(String key) {
    _box.remove(key);
  }

  setSystemTip(int tip) => _box.write(StoreKeys.systemTip.name, tip);

  int? getSystemTip() => _box.read<int>(StoreKeys.systemTip.name);

  setOrderMsgTip(int tip) => _box.write(StoreKeys.orderMsgTip.name, tip);

  int? getOrderMsgTip() => _box.read<int>(StoreKeys.orderMsgTip.name);

  setAccount(String callId) async {
    var res = await _box.write("account_key", callId);
  }

  String? getAccount() => _box.read<String>("account_key");

  setPassword(String callId) => _box.write(StoreKeys.password.name, callId);

  String? getPassword() => _box.read<String>(StoreKeys.password.name);

  setLanguage(String lan) => _box.write(StoreKeys.language.name, lan);
  String? getLanguage() => _box.read<String>(StoreKeys.language.name);

  setCurrency(String code) => _box.write(StoreKeys.currency.name, code);
  String? getCurrency() => _box.read<String>(StoreKeys.currency.name);

  setPermission(Map<String, dynamic> permiss) =>
      _box.write("permissoin", permiss);

  Map<String, dynamic> getPermissoin() =>
      _box.read("permissoin") ?? {};
}
